<script setup>
	import { onLaunch, onShow, onHide } from '@dcloudio/uni-app';
	import { storeToRefs } from 'pinia'
    import { userStore } from '@/stores'

	onLaunch(() => {
		// #ifdef APP-PLUS
		plus.screen.lockOrientation("portrait-primary");
		// #endif
		const token = uni.getStorageSync('token');
		const user = userStore()
		const { userId, getInfo } = user
		console.log('用户', userId);
		if(!token) {
			uni.reLaunch({
				url: '/pages/login/index',
				success: function() {
					// #ifdef APP-PLUS
					plus.navigator.closeSplashscreen()
					// #endif
				}
			})
		}else if (!userId && token) {
			getInfo().then((res) => {
				uni.switchTab({
					url: '/pages/home/index',
					complete: () => {
						console.log('come in...');
						// #ifdef APP-PLUS
						plus.navigator.closeSplashscreen()
						// #endif
					}
				})
			}).catch(() => {
				uni.reLaunch({
					url: '/pages/login/index',
					success: function() {
						console.log('error out...');
						// #ifdef APP-PLUS
						plus.navigator.closeSplashscreen()
						// #endif
					}
				})
			})
		}
		console.log('App Launch')
	}),
	onShow(() => {
		uni.onNetworkStatusChange(function(res) {
			if (res.isConnected === false) {
				uni.showToast({
					title: '网络连接失败,请检查您的网络环境...',
					icon: 'none'
				})
				clearLoginInfo()
			}
		});
		console.log('App Show')
	}),
	onHide(() => {
		console.log('App Hide')
	})
</script>

<style>
	/*每个页面公共css */
</style>
